package api

import (
	"singo/service"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

// DefectCreate 新增缺陷
// @Summary 新增缺陷
// @Router /api/v1/defect/create [post]
// @tags 缺陷相关
// @accept application/json
// @Param name query string true "缺陷名称"
// @Param intro query string false "介绍"
// @Param project_id query string false "关联项目ID"
// @Param organization_id query string false "关联组织ID"
// @Param status query int false "状态"
// @Param priority query int false "优先级"
// @Param created_by query int false "创建人"
// @Param execute_by query int false "执行人"
// @Param deadline_at query string false "截止日期"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func DefectCreate(c *gin.Context) {
	var service service.DefectCreateService
	session := sessions.Default(c)
	userUID := session.Get("user_uid")

	if err := c.ShouldBind(&service); err == nil {
		res := service.DefectCreate(userUID.(string))
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// DefectList 缺陷列表接口
// @Summary 缺陷列表
// @Router /api/v1/defects [get]
// @tags 缺陷相关
// @accept application/json
// @Param user_id query string false "关联用户ID"
// @Param organization_id query string false "关联小组ID"
// @Param project_id query string false "关联项目ID"
// @Param status query int false "状态"
// @Param pageNo query int false "页码"
// @Param pageSize query int false "条数"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func DefectList(c *gin.Context) {
	service := service.ListDefectService{}
	if err := c.ShouldBind(&service); err == nil {
		res := service.DefectList()
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// DefectShow 缺陷详情接口
// @Summary 缺陷详情
// @Router /api/v1/defect/{id} [get]
// @tags 缺陷相关
// @accept application/json
// @Param id path string false "缺陷ID"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func DefectShow(c *gin.Context) {
	service := service.DefectShowService{}
	res := service.DefectShow(c.Param("id"))
	c.JSON(200, res)
}

// DefectUpdate 更新缺陷接口
// @Summary 更新缺陷
// @Router /api/v1/defect/{id} [put]
// @tags 缺陷相关
// @accept application/json
// @Param id path string false "缺陷ID"
// @Param name query string false "缺陷名称"
// @Param intro query string false "缺陷介绍"
// @Param priority query int false "优先级"
// @Param status query int false "状态"
// @Param project_id query string false "关联项目ID"
// @Param organization_id query string false "关联组织ID"
// @Param execute_by query string false "执行人"
// @Param deadline_at query string false "截止日期"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func DefectUpdate(c *gin.Context) {
	service := service.DefectUpdateService{}
	if err := c.ShouldBind(&service); err == nil {
		res := service.DefectUpdate()
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// DefectDelete 删除缺陷接口
// @Summary 删除缺陷
// @Router /api/v1/defect/{id} [delete]
// @tags 缺陷相关
// @accept application/json
// @Param id path string false "缺陷ID"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func DefectDelete(c *gin.Context) {
	service := service.DefectDeleteService{}
	res := service.DefectDelete(c.Param("id"))
	c.JSON(200, res)
}
